package com.hamster.crmpre.web;

import com.hamster.crmpre.mapper.CustomerMapper;
import com.hamster.crmpre.mapper.ProjectMapper;
import com.hamster.crmpre.pojo.Customer;
import com.hamster.crmpre.pojo.CustomerCourseClass;
import com.hamster.crmpre.pojo.HandOverCustomerAssets;
import com.hamster.crmpre.pojo.Project;
import com.hamster.crmpre.pojo.dto.ProjectCourseClassTime;
import com.hamster.crmpre.service.CustomerAssetsService;
import com.hamster.crmpre.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 转交客户资产的Controller
 * @author Tarry
 * @create 2020/1/3 13:53
 */
@RestController
@RequestMapping("handOverCustomerAssets")
public class HandOverCustomerAssetsController {
    @Autowired
    private CustomerAssetsService customerAssetsService;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ProjectMapper projectMapper;
    /**
     * 转交客户 资产
     * customerId  原始客户id
     * customerName 原始客户姓名
     * select  0
     * content 选择的要转交的课程  json
     * 等....
     * @return
     */
    @PostMapping("submit")
    public ResponseEntity<Void> submitCustomerAssets(@RequestBody HandOverCustomerAssets handOverCustomerAssets){
        String customerId = handOverCustomerAssets.getCustomerId();//原始客户的id
        String transferCustomerId = handOverCustomerAssets.getTransferCustomerId();//转至客户的id
        String customerName = customerMapper.selectById(customerId).getName();//原始客户姓名

        String transferCustomerName = null;
        //如果转至客户id为空，则不进行一下操作
        if (StringUtils.isNotBlank(transferCustomerId)){
            transferCustomerName = customerMapper.selectById(transferCustomerId).getName();//目标客户的姓名
            handOverCustomerAssets.setCustomerName(customerName);
        }
        handOverCustomerAssets.setTransferCustomerName(transferCustomerName);
        customerAssetsService.submitCustomerAssets(handOverCustomerAssets);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取页面课程列表
     * @param customerId
     * @return
     */
    @GetMapping("getCourseDetails")
    public ResponseEntity<List<ProjectCourseClassTime>> getCourseDetails(@RequestParam String customerId){
        List<CustomerCourseClass> list = customerAssetsService.getCourseDetails(customerId);
        //上面代码，找出了课程及上课时间。下面代码 把课程放入规定的 项目内
        Customer customer = customerMapper.selectById(customerId);
        List<String> projectList = JsonUtils.parseList(customer.getProjectId(), String.class);
        List<ProjectCourseClassTime> projectCourseClassTimeList = new ArrayList<>();
        for (String pid : projectList) {
            ProjectCourseClassTime projectCourseClassTime = new ProjectCourseClassTime();
            Project project = projectMapper.selectById(pid);
            projectCourseClassTime.setProjectId(pid);
            projectCourseClassTime.setProjectTitle(project.getTitle());
            List<CustomerCourseClass> courseList = new ArrayList<>();
            for (CustomerCourseClass customerCourseClass : list) {
                if (pid.equals(customerCourseClass.getProjectId())){
                    courseList.add(customerCourseClass);
                }
            }
            projectCourseClassTime.setCourseList(courseList);
            projectCourseClassTimeList.add(projectCourseClassTime);
        }
        return ResponseEntity.ok(projectCourseClassTimeList);
    }

    /**
     * 获取客户select 下拉框，选择要转交的目标客户
     * @return
     */
    @GetMapping("selectCustomer")
    public ResponseEntity<List<Customer>> selectCustomer(){
        List<Customer> list = customerAssetsService.selectCustomer();
        return ResponseEntity.ok(list);
    }



}
